<?php
session_save_path('sessions');
session_start();

// Connect to our database
include("include/dbconnection.php");

// Include our user class
include_once("include/user.php");


// Search info sent from form
$city = $_POST['city'];
$postalcode = $_POST['postalCode'];
$neighbourhood = $_POST['neighbourhood'];
$country = $_POST['country'];
$pricemin = $_POST['priceMin'];
$pricemax = $_POST['priceMax'];
$sizemin = $_POST['sizeMin'];
$sizemax = $_POST['sizeMax'];
$yearmin = $_POST['yearMin'];
$yearmax = $_POST['yearMax'];
$propertytype1 = $_POST['propertyType1'];
$propertytype2 = $_POST['propertyType2'];
$propertytype3 = $_POST['propertyType3'];
$propertytype4 = $_POST['propertyType4'];
$heating = $_POST['heating'];
$cooling = $_POST['cooling'];
$garage = $_POST['garage'];
$driveway = $_POST['driveway'];
$roomnumber = $_POST['roomnumber'];
$other = $_POST['other'];


// To protect MySQL injection (more detail about MySQL injection)
$city = stripslashes($city);
$postalcode = stripslashes($postalcode);
$neighbourhood = stripslashes($neighbourhood);
$country = stripslashes($country);
$pricemin = stripslashes($pricemin);
$pricemax = stripslashes($pricemax);
$sizemin = stripslashes($sizemin);
$sizemax = stripslashes($sizemax);
$yearmin = stripslashes($yearmin);
$yearmax = stripslashes($yearmax);
$propertytype1 = stripslashes($propertytype1);
$propertytype2 = stripslashes($propertytype2);
$propertytype3 = stripslashes($propertytype3);
$propertytype4 = stripslashes($propertytype4);
$heating = stripslashes($heating);
$cooling = stripslashes($cooling);
$garage = stripslashes($garage);
$driveway = stripslashes($driveway);
$roomnumber = stripslashes($roomnumber);
$other = stripslashes($other);

?>
<?php include("include/header.php"); ?>
<html>
<body>
  <div id="wrapper">
    <div id="header"> </div>
    <div id="left">
      <div id="logo">
        <h1>Home Sale</h1>
        <p>Only By Owner</p>
      </div>
      <div id="nav">
        <ul>
          <?php 

		 if(isset($_SESSION['user']))
			{
				?>

				<li>
						<a href="addAhomeForSale.php">Add a House for sale</a>
					</li>
					<li>
						<a href="updateAhome.php">Update House information</a>
					</li>
					<li >
						<a href="updateAhomePicture.php">Update House picture</a>
					<li class="important">
						<a href="searchhouse.php">Search House</a></li>
					<li>
						<a href="emailMenu.php">Mailbox</a>
					</li>
					<li>
						<a href="logout.php">Logout</a>
				</li>
			<?php
				}
				else{
				?>
					  <li ><a href="index.php">Home</a></li>
					  <li><a href="main_login.php">Login</a></li>
					  <li><a href="register.php">Register</a></li>
					  <li class="important"><a href="searchhouse.php">Search House</a></li>
					  <li><a href="logout.php">Logout</a></li>

				<?php
						}
					?>
        </ul>
      </div>
      <div id="news">
        <h2>Latest News</h2>
        <?php include("adgeneratorleft1.php");?>
        <div class="hr-dots"> </div>
        <?php include("adgeneratorleft2.php");?>
        <p class="more"><a href="index.php">more</a></p>
      </div>
      <div id="support">
        <p>gnc353_2@encs.concordia.ca</p>
      </div>
    </div>
    <div id="right">
      <h2>Search Results</h2>
      <div id="welcome">
<?php
if($pricemin >= $pricemax)
{
  echo "Price range is incorrect!";
}
else if($sizemin >= $sizemax)
{
  echo "Size range is incorrect!";
}
else if($yearmin >= $yearmax)
{
  echo "Year built range is incorrect!";
}
else
{
$db_query = "SELECT 
    h.$TABLE_HOME_HOMEID, h.$TABLE_HOME_ADDRESS, h.$TABLE_HOME_STREETNAME, 
    h.$TABLE_HOME_CITY, h.$TABLE_HOME_COUNTRY, h.$TABLE_HOME_DISTRICT, h.$TABLE_HOME_ZONE,
	h.$TABLE_HOME_POSTALCODE, h.$TABLE_HOME_LOTSIZE, h.$TABLE_HOME_PROPERTYTYPE, h.$TABLE_HOME_YEARBUILT,
	h.$TABLE_HOME_HEATING, h.$TABLE_HOME_COOLING, h.$TABLE_HOME_PARKING, h.$TABLE_HOME_GARAGE,
	h.$TABLE_HOME_DRIVEWAY, h.$TABLE_HOME_NEIGHBOURHOOD, h.$TABLE_HOME_IMPROVEMENTS, h.$TABLE_HOME_EVALUATION,
	h.$TABLE_HOME_TAXES, h.$TABLE_HOME_UTILITYCOST, h.$TABLE_HOME_PRICE, h.$TABLE_HOME_COMMENTS,
	h.$TABLE_HOME_LISTINGDATE
	
    FROM $TABLE_HOME h 
    

    WHERE     
	
 city LIKE '%$city%' AND 
	 postalCode LIKE '%$postalcode%' AND
	 neighbourhood LIKE '%$neighbourhood%' AND
	 country LIKE '%$country%' AND
	 price>'$pricemin' AND
	 price<'$pricemax' AND
	 lotSize>'$sizemin' AND
	 lotSize<'$sizemax' AND
	 yearBuilt>'$yearmin' AND
	 yearBuilt<'$yearmax' AND
     comments LIKE '%$other%' AND 
	 heating='$heating' AND
	 cooling='$cooling' AND
	 garage='$garage' AND
	 driveway='$driveway' AND
	 (propertyType LIKE '$propertytype1' OR
	 '$propertytype2' OR 
	 '$propertytype3' OR 
	 '$propertytype4') AND
	 h.$TABLE_HOME_SOLDTO = 0 
	";
}
    
$resultSet = mysql_query($db_query);

if(mysql_num_rows($resultSet) == 0)
{
  echo "No matches found!";
}
else{ ?>

        <?php
        // Show all the users and add an activation link.
        while( $result = mysql_fetch_array($resultSet) ) {
            ?>
      <table>
      <tr><td><b>HomeID:</b></td><td><b><a href="displayHome.php?id=<?php echo $result[$TABLE_HOME_HOMEID]?>"><?php echo $result[$TABLE_HOME_HOMEID]?></a></b></td></tr>			
			<tr>
				<td><b>Size:</b></td>
				<td><?php echo $result[$TABLE_HOME_LOTSIZE]?></td>
			</tr>
			<tr><td><b>Property Type:</b></td><td>
        <?php 
		switch($result[$TABLE_HOME_PROPERTYTYPE]){
        case 1:
        {
          echo "Condo";
          break;
        }
        case 2:
        {
          echo "Apartment";
          break;
        }
        case 3:
        {
          echo "Single family";
          break;
        }
        case 4:
        {
          echo "Cottages";
          break;
        }
		}
        ?>
		</td><td><b>Year built:</b></td>
				<td><?php echo $result[$TABLE_HOME_YEARBUILT]?></td></tr>
			<tr><td><b>Heating:</b></td><td>
			<?php if($result[$TABLE_HOME_HEATING]){
					echo "Yes";
				}
				else
				{
					echo "No";
				}
			 ?></td><td><b>Cooling:</b></td><td><?php if($result[$TABLE_HOME_COOLING]){
					echo "Yes";
				}
				else
				{
					echo "No";
				}
			 ?></td></tr>
			<tr><td><b>Parking:</b></td><td><?php if($result[$TABLE_HOME_PARKING]){
					echo "Yes";
				}
				else
				{
					echo "No";
				}
			 ?></td><td><b>Driveway:</b></td><td><?php if($result[$TABLE_HOME_DRIVEWAY]){
				echo "Yes";
				}
					else
				{
					echo "No";
				}
			 ?></td></tr>
			<tr><td><b>Neighbourhood:</b></td><td><?php echo $result[$TABLE_HOME_NEIGHBOURHOOD] ?></td></tr>
			<tr><td><b>Improvements:</b></td><td><?php echo $result[$TABLE_HOME_IMPROVEMENTS] ?></td></tr>
			<tr>
				<td><b>Evaluation:</b></td>
				<td><?php echo $result[$TABLE_HOME_EVALUATION]?></td>
			</tr>
			<tr>
				<td><b>Utility Cost:</b></td>
				<td><?php echo $result[$TABLE_HOME_UTILITYCOST]?></td>
			</tr>
			<tr>
				<td><b>Price:</b></td>
				<td><?php echo $result[$TABLE_HOME_PRICE]?></td>
			</tr>
			<tr><td><b>Comments:</b></td><td><?php echo $result[$TABLE_HOME_COMMENTS]?></td></tr>
			<tr>
				<td><b>Posted:</b></td>
				<td><?php echo date("d/m/Y", $result[$TABLE_HOME_LISTINGDATE])?></td>
            </tr>
      </table>
            <?php
      }// End while loop ?>
<?php
} // End else statement
    
    // Close the connection
    mysql_close();
?>
      </div>
      <h3></h3>
      <div id="profile">
        <div id="corp">
          <?php include("adgeneratorbottomleft.php");?>
        </div>
        <div id="indu">
          <?php include("adgeneratorbottomright.php");?>
        </div>
      </div>
    </div>
    <div class="clear"> </div>
    <div id="spacer"> </div>
    <div id="footer">
      <div id="copyright">
        &copy; Template taken from http://www.freewebsitetemplates.com/
      </div>
	  <div id="footerline"></div>
    </div>	
  </div>
</body>
</html>
